/**
 * Created by Administrator on 2017/6/27 0027.
 */
import React from "react";
import {Checkbox,message,Table} from 'antd';
import Relay from "react-relay";
import AaaESMItem from "../AaaESMItem"
import ReactKey from "../../../util/ReactKey"
import Confirm from "../../../common/Confirm"
import Util from "../../../util/Util"
import {Modal} from 'fly';
import FindMaterielBillMutation from "./mutation/FindMaterielBillMutation"
export default class AddMaterialModal extends React.Component{
    constructor(props){
        super(props)
        this.state={
            visible:false,
            goodsInfoDescModelList: [],
            orderId:'',
            dataSource :[],
            selectedRowKeys:[],
            selectedRows:[],
        }
        this.orders=[];
        this.enterpriseId = sessionStorage.getItem('enterpriseId');
        this.columns = [{
            title: '款号',
            dataIndex: 'styleNo',
            key: 'styleNo',
            width:72
        }, {
            title: '品名',
            dataIndex: 'productName',
            key: 'productName',
            width:164
        },{
            title: '颜色',
            dataIndex: 'color',
            key: 'color',
            width:164
        }, {
            title: '描述/规格',
            dataIndex: 'description',
            key: 'description',
            width:310
        }, {
            title: '需求数',
            dataIndex: 'needCount',
            key: 'needCount',
            width:220
        }, {
            title: '单位',
            dataIndex: 'prickle',
            key: 'prickle',
            width:148
        }];

    }



    componentWillMount(){
    }
    
    requireData(name){
        let materielType;
        if(name=='cloth'){
            materielType = 1
        }else{
            materielType = '2,3'
        }


        Relay.Store.commitUpdate(
            new FindMaterielBillMutation({
                orderId : this.state.orderId,
                purchStatus :0,//未采购
                materielType:materielType,
                pageNum:1,
                pageSize:1000,
            }),
            {
                onFailure: () => {
                    message.config({
                        top: 30,
                    })
                    message.error('网络请求失败');
                },
                onSuccess: (response) => {
                    let data = response.findMaterielBill.findMaterielBill
                    let errorcode = data.errorCode;
                    if(errorcode==0){
                        let goodsInfoDescModelList = data.materielBillList
                        if(!goodsInfoDescModelList){
                            goodsInfoDescModelList = []
                        }
                        let dataSource = []
                        goodsInfoDescModelList.map((context,index)=>{
                            dataSource.push({key:index,
                                bomId:context.bomId,
                                styleNo:context.styleNo,
                                productName:context.productName,
                                color:context.color,
                                description:context.description,
                                needCount:context.needCount,
                                prickle:context.prickle});
                        });

                        this.setState({
                            dataSource:dataSource
                        });
                    }
                }
            }
        )
    }



    closemodal(){
        this.cleanData();
    }



    sure(){
        let dataIndex = this.state.selectedRowKeys;
        let selectedRows = JSON.parse(JSON.stringify(this.state.selectedRows));
        let dataSource = this.state.dataSource;
        let data = [];
        if(dataIndex.length<=0){
            message.config({
                top: 30,
            })
            message.info('请选择需要导入的数据');
            return
        }
        selectedRows.map((context)=>{
            context.colour = context.color;
            context.quantity = context.needCount;
        });
        this.props.toLeadExcel(selectedRows);
        this.cleanData();
    }

    afterClose(){
        this.cleanData();
    }

    onChange(selectedRowKeys,selectedRows){
        this.setState({
            selectedRowKeys,
            selectedRows,
        });
    }


    //清除数据
    cleanData(){
        setTimeout(() => {
            this.setState({
                selectedRowKeys: [],
                selectedRows: [],
                visible: false,
            });
        }, 10);
    }

    render(){
        let { selectedRowKeys } = this.state;
        let rowSelection = {
            selectedRowKeys,
            onChange : this.onChange.bind(this),
        };



        return (
            <Modal
                wrapClassName="batchhint_modal materielBom_table"
                visible={this.state.visible}
                title={'导入商品'}
                onOk={this.sure.bind(this)}
                onCancel={this.closemodal.bind(this)}
                width={1200} style={{left: '5%'}}
                afterClose={this.afterClose.bind(this)}>
                <Table
                    rowKey={record =>record.bomId}
                    columns={this.columns}
                    className="order_table"
                    //bordered
                    scroll={{y: 240}}
                    pagination={false}
                    dataSource={this.state.dataSource}
                    rowSelection={rowSelection}/>
            </Modal>
        )
    }
}

